package com.cnunicom.modules.business.controller;

import java.util.*;

import com.cnunicom.modules.business.entity.BusinessDeptEntity;
import com.cnunicom.modules.business.service.BusinessDeptService;
import com.cnunicom.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.business.entity.BusinessWorkEntity;
import com.cnunicom.modules.business.service.BusinessWorkService;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;

import javax.annotation.Resource;


/**
 * 业务工作表
 *
 * @author dingziyuan
 * @email dzy998877@163.com
 * @date 2019-07-19 10:52:15
 */
@RestController
@RequestMapping("business/businesswork")
public class BusinessWorkController extends AbstractController {
    @Autowired
    private BusinessWorkService businessWorkService;

    @Resource
    private BusinessDeptService businessDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businesswork:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("user", getUser());
        PageUtils page = businessWorkService.queryPage(params);

        List<BusinessDeptEntity> list = businessDeptService.list();
        return Objects.requireNonNull(R.ok().put("page", page)).put("deptList", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:businesswork:info")
    public R info(@PathVariable("id") Long id){
        BusinessWorkEntity businessWork = businessWorkService.getById(id);

        return R.ok().put("businessWork", businessWork);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businesswork:save")
    public R save(@RequestBody BusinessWorkEntity businessWork){
        Date date = new Date();
        businessWork.setCreateTime(date);
        businessWork.setUpdateTime(date);
        businessWork.setCreateUserId(getUserId());
		businessWorkService.save(businessWork);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businesswork:update")
    public R update(@RequestBody BusinessWorkEntity businessWork){
        businessWork.setUpdateTime(new Date());
		businessWorkService.updateById(businessWork);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businesswork:delete")
    public R delete(@RequestBody Long[] ids){
		businessWorkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
